using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace GDrive {
	class GDriveTest : Form {
		public TextBox ctxtAuthURL, ctxtAccessCode;
		public Label clblMessage, clblList;

		public OAuth2Parameters cParams;

		public static void Main(string[] astrArgs) {
			GDriveTest myForm;

			myForm = new GDriveTest();

			Application.Run(myForm);
		}

		public GDriveTest() {
			Button btnSubmit;

			//Create the controls
			clblMessage = new Label();
			clblMessage.Height = 18;
			clblMessage.Width = 300;
			clblMessage.Top = 0;
			clblMessage.Left = 0;

			ctxtAuthURL = new TextBox();
			ctxtAuthURL.Height = 18;
			ctxtAuthURL.Width = 300;
			ctxtAuthURL.Top = 20;
			ctxtAuthURL.Left = 0;

			ctxtAccessCode = new TextBox();
			ctxtAccessCode.Height = 18;
			ctxtAccessCode.Width = 300;
			ctxtAccessCode.Top = 40;
			ctxtAccessCode.Left = 0;
			
			btnSubmit = new Button();
			btnSubmit.Height = 18;
			btnSubmit.Width = 50;
			btnSubmit.Top = 60;
			btnSubmit.Left = 0;
			btnSubmit.Text = "Submit Code";
			btnSubmit.Click += new EventHandler(SubmitAccessCode);

			clblList = new Label();
			clblList.Height = 200;
			clblList.Width = 300;
			clblList.Top = 80;
			clblList.Left = 0;

			//Position and configure the form
			this.Width = 307;
			this.Height = 307;
			this.FormBorderStyle = FormBorderStyle.FixedSingle;

			Controls.Add(clblMessage);
			Controls.Add(ctxtAuthURL);
			Controls.Add(ctxtAccessCode);
			Controls.Add(btnSubmit);

			//Create the authentication parameters
			cParams = new OAuth2Parameters();

			cParams.ClientId = "1056112142424-3ol8600gshud8q8iknce4ff9506ofuee.apps.googleusercontent.com";
			cParams.ClientSecret = "gqljplTh6Va75ZbhL_v76SEj";
			cParams.RedirectUri = "urn:ietf:wg:oauth:2.0:oob";
			cParams.Scope = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
			ctxtAuthURL.Text = OAuthUtil.CreateOAuth2AuthorizationUrl(cParams);
		}

		private void SubmitAccessCode(object oSender, EventArgs eaArgs) {
			GOAuth2RequestFactory reqFactory;
			SpreadsheetsService gsService;
			SpreadsheetQuery sqQuery;
			SpreadsheetFeed sfFeed;

			cParams.AccessCode = ctxtAccessCode.Text;
			OAuthUtil.GetAccessToken(cParams);

			reqFactory = new GOAuth2RequestFactory(null, "", cParams);
			gsService = new SpreadsheetsService("");
			sqQuery = new SpreadsheetQuery();

			gsService.RequestFactory = reqFactory;

			sfFeed = gsService.Query(sqQuery);

			clblList.Text = "";
			foreach (SpreadsheetEntry seEntry in sfFeed.Entries) {
				clblList.Text = clblList.Text + "\n" + seEntry.Title.Text;
			}
		}
	}
}